<?php
/**
 * PhpStorm
 * User: zucheng
 * Date: 2019/5/6
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\Shipping;
use Illuminate\Http\Request;

class ShippingController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $status = Shipping::$statusDesc;
        return view('admin.shipping.index', compact('status'));
    }

    public function data(Request $request)
    {
        $model = Shipping::query();
        if ($request->get('start_time')) {
            $model = $model->where('created_at', '>=', $request->get('start_time'));
        }
        if ($request->get('end_time')) {
            $model = $model->where('created_at', '<=', $request->get('end_time'));
        }
        if ($request->get('shipping_no')) {
            $model = $model->where('shipping_no', $request->get('shipping_no'));
        }
        if ($request->get('status')) {
            $model = $model->where('status', $request->get('status'));
        }
        $res = $model->with(['driver'])
            ->orderBy('created_at', 'desc')
            ->paginate($request->get('limit', 30))
            ->toArray();
        if ($res['total'] > 0) {
            foreach ($res['data'] as $key => &$item) {
                $item['global_desc'] = Shipping::$globalDesc[ $item['global_status'] ];
                $item['status_desc'] = Shipping::$statusDesc[ $item['status'] ];
            }
        }

        return $this->table($res['data'], $res['total']);
    }

}